package com.itheima.sfbx.insurance.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.sfbx.framework.commons.constant.basic.SuperConstant;
import com.itheima.sfbx.insurance.enums.RiskAnalysisEnum;
import com.itheima.sfbx.insurance.pojo.CategorySafeguard;
import com.itheima.sfbx.insurance.pojo.PlanSafeguard;
import com.itheima.sfbx.insurance.pojo.Safeguard;
import com.itheima.sfbx.insurance.mapper.SafeguardMapper;
import com.itheima.sfbx.insurance.service.ICategorySafeguardService;
import com.itheima.sfbx.insurance.service.IPlanSafeguardService;
import com.itheima.sfbx.insurance.service.ISafeguardService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.transaction.annotation.Transactional;
import com.itheima.sfbx.insurance.constant.SafeguardCacheConstant;
import com.itheima.sfbx.insurance.dto.SafeguardVO;
import com.itheima.sfbx.insurance.enums.SafeguardEnum;
import com.itheima.sfbx.framework.commons.utils.BeanConv;
import com.itheima.sfbx.framework.commons.utils.EmptyUtil;
import com.itheima.sfbx.framework.commons.utils.ExceptionsUtil;
import com.itheima.sfbx.framework.commons.exception.ProjectException;
import com.google.common.collect.Lists;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import lombok.extern.slf4j.Slf4j;
/**
 * @Description：保障项服务实现类
 */
@Slf4j
@Service
public class SafeguardServiceImpl extends ServiceImpl<SafeguardMapper, Safeguard> implements ISafeguardService {
    @Autowired
    private ICategorySafeguardService categorySafeguardService;
    @Autowired
    private IPlanSafeguardService planSafeguardService;
    //2025.7.25实现分页查询
    @Override
    public Page<SafeguardVO> findPage(SafeguardVO safeguardVO, int pageNum, int pageSize) {
        //TODO
        Page<Safeguard> page = lambdaQuery()
                .eq(StrUtil.isNotBlank(safeguardVO.getSafeguardKey()),Safeguard::getSafeguardKey, safeguardVO.getSafeguardKey())
                .like(StrUtil.isNotBlank(safeguardVO.getSafeguardKeyName()),Safeguard::getSafeguardKeyName, safeguardVO.getSafeguardKeyName())
                .eq(safeguardVO.getSortNo() != null,Safeguard::getSortNo, safeguardVO.getSortNo())
                .page(new Page<>(pageNum, pageSize));
        Page<SafeguardVO> pageResult = BeanConv.toPage(page, SafeguardVO.class);
        return pageResult;
    }

    @Override
    public SafeguardVO findById(String safeguardId) {
        //TODO
        return null;
    }
    //2025.7.25实现新增
    @Override
    @Transactional
    public SafeguardVO save(SafeguardVO safeguardVO) {
        //TODO
        Safeguard safeguard = BeanConv.toBean(safeguardVO, Safeguard.class);
        //select * from tab_safeguard where
        Integer count = lambdaQuery()
                .eq(Safeguard::getSafeguardKey, safeguardVO.getSafeguardKey())
                .count();
        if (count > 0){
            log.error("key重复，新增失败");
            throw new ProjectException(RiskAnalysisEnum.SAVE_FAIL);
        }
        this.save(safeguard);
        SafeguardVO safeGuardVO = BeanConv.toBean(safeguard, SafeguardVO.class);
        return safeGuardVO;
    }
    //2025.7.25实现修改
    @Override
    @Transactional
    public Boolean update(SafeguardVO safeguardVO) {
        //TODO
        Integer count = lambdaQuery()
                .eq(Safeguard::getSafeguardKey, safeguardVO.getSafeguardKey())
                .ne(Safeguard::getId, safeguardVO.getId())
                .count();
        if (count>0){
            log.error("keyName重复，更新失败");
            throw new ProjectException(RiskAnalysisEnum.UPDATE_FAIL);
        }
        Integer count1 = categorySafeguardService.lambdaQuery()
                .eq(CategorySafeguard::getSafeguardKey, safeguardVO.getSafeguardKey())
                .count();
        Integer count2 = planSafeguardService.lambdaQuery()
                .eq(PlanSafeguard::getSafeguardKey, safeguardVO.getSafeguardKey())
                .count();
        if (count1>0 || count2>0){
            log.error("key重复，更新失败");
            throw new ProjectException(RiskAnalysisEnum.UPDATE_FAIL);
        }
        Safeguard safeguard = BeanConv.toBean(safeguardVO, Safeguard.class);
        updateById(safeguard);
        return true;
    }
    //2025.7.25实现删除
    @Override
    @Transactional
   public Boolean delete(String[] checkedIds) {
        //TODO
        Set<Long> ids = Arrays.stream(checkedIds).map(Long::valueOf).collect(Collectors.toSet());
        if (!CollUtil.isNotEmpty(ids)){
            return true;
        }
        List<Safeguard> list = lambdaQuery()
                .in(Safeguard::getId, ids)
                .list();
        Set<String> keySet = list.stream().map(Safeguard::getSafeguardKey).collect(Collectors.toSet());
        Integer count1 = categorySafeguardService.lambdaQuery()
                .in(CategorySafeguard::getSafeguardKey, keySet)
                .count();
        Integer count2 = planSafeguardService.lambdaQuery()
                .in(PlanSafeguard::getSafeguardKey, keySet)
                .count();
        if (count1>0 || count2>0){
            log.error("修改失败");
            throw new ProjectException(RiskAnalysisEnum.DEL_FAIL);
        }
        removeByIds(ids);
        return true;
    }

    @Override
    public List<SafeguardVO> findList(SafeguardVO safeguardVO) {
        //TODO
        return null;
    }


    @Override
    public List<SafeguardVO> findShowPageItemByKey(List<String> safeguardKeyList) {
        //TODO
        return null;
    }

    @Override
    public SafeguardVO findBySafeguardKey(String safeguardKey) {
        //TODO
        return null;
    }
}
